package cn.cyber.wxfire.main;

import cn.cyber.wxfire.http.WXHttpResult;
import cn.cyber.wxfire.service.AccessTokenService;
import cn.cyber.wxfire.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author gjn
 * @since 2021/9/17 23:53
 */
@Component
@Slf4j
public class WXInit implements ApplicationRunner {

    @Resource
    private AccessTokenService tokenService;
    @Resource
    private MenuService menuService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        WXHttpResult result;
        String tokenStr  = tokenService.getAccessTokenStr();
        if (tokenStr != null) {
            if (menuService.hasMenu()) {
                result = menuService.delMenu();
                if (("0".equals(result.getErrcode())) && ("ok".equals(result.getErrmsg()))) {
                    log.info("原有菜单删除成功！");
                } else {
                    log.error("原有菜单删除失败！");
                }
            }
            result = menuService.setMenu();
            if ( ("0".equals(result.getErrcode()))&& ("ok".equals(result.getErrmsg())) ) {
                log.info("创建菜单成功！");
            } else {
                log.error("创建菜单失败！");
            }
        }
    }
}
